package org.hiucung.orangemusic.dialog;

import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import org.hiucung.orangemusic.helper.MediaPlayerHelper;
import org.hiucung.orangemusic.R;
import org.hiucung.orangemusic.adapter.MusicAdapter;
import org.hiucung.orangemusic.bean.MusicBean;

import java.util.ArrayList;

public class MusicListBottomDialog extends BottomSheetDialogFragment{

    private Context mContext;
    private View view;
    ArrayList<MusicBean> mDatas;//数据源
    private MusicAdapter adapter;//适配器

    MediaPlayerHelper mediaPlayerHelper;

    MusicBean musicBean;

    int currnetPlayPosition;

    LinearLayoutManager layoutManager;//设置布局管理器

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (getActivity() == null) return super.onCreateDialog(savedInstanceState);

        BottomSheetDialog dialog = new BottomSheetDialog(getActivity(), R.style.Theme_MaterialComponents_BottomSheetDialog);

        dialog.setContentView(R.layout.music_list_fragment_layout);

        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        //获取dialog对象
        BottomSheetDialog dialog = (BottomSheetDialog) getDialog();

        //获取diglog的根部局
        FrameLayout bottomSheet = dialog.getDelegate().findViewById(R.id.design_bottom_sheet);
        if (bottomSheet != null) {
            //获取根部局的LayoutParams对象
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();
            layoutParams.height = getPeekHeight();
            //修改弹窗的最大高度，不允许上滑（默认可以上滑）
            bottomSheet.setLayoutParams(layoutParams);

            //这个是设置当触发时初始弹窗的高度
            final BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet);
            //peekHeight即弹窗的最大高度
            behavior.setPeekHeight(getPeekHeight());
            // 初始为展开状态
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }

    /**
     * 弹窗高度，默认为屏幕高度的四分之三
     * 子类可重写该方法返回peekHeight
     *
     * @return height
     */
    protected int getPeekHeight() {
        int peekHeight = getResources().getDisplayMetrics().heightPixels;
        //设置弹窗高度为屏幕高度的3/4
        return peekHeight/4*3;
    }

    @Override
    public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,  @Nullable Bundle savedInstanceState){
        mContext = getContext();
        Log.e("TAG","onCreateView: ");
        view = inflater.inflate(R.layout.music_list_fragment_layout,container,false);
        initData();
        initViews(view);
        setEventListener();
        return view;
    }

    private void initViews(View view) {
        RecyclerView musicRv = view.findViewById(R.id.local_music_rv);
        //创建适配器
        adapter = new MusicAdapter(mContext,mDatas);
        musicRv.setAdapter(adapter);
        //设置布局管理器
        layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        musicRv.setLayoutManager(layoutManager);
    }

    private void initData() {
        mediaPlayerHelper = MediaPlayerHelper.getInstance(getActivity());
        mDatas = new ArrayList<>();
        Bundle bundle = this.getArguments();//得到从Activity传来的数据
        mDatas = (ArrayList<MusicBean>) bundle.getSerializable("mDatas");

    }

    /***
     * 点击事件通过实现接口实现类
     */
    private void setEventListener() {
        adapter.setOnItemClickListener(new MusicAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                currnetPlayPosition = position;
                musicBean = mDatas.get(position);
                playMusicInBean(musicBean);
            }
        });
    }

    private void playMusicInBean(MusicBean musicBean) {
        mediaPlayerHelper.setPath(musicBean.getPath());
        if (mediaPlayerHelper.isPlaying()) {
            mediaPlayerHelper.setOnMeidaPlayerHelperListener(new MediaPlayerHelper.OnMeidaPlayerHelperListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayerHelper.start();
                }

                @Override
                public void onCompletion(MediaPlayer mp) {

                }
            });
        }
    }
}


